package GradeManager.StudentGradeSystem.service.imp;

import GradeManager.StudentGradeSystem.common.BaseContent;
import GradeManager.StudentGradeSystem.dao.PersonDao;
import GradeManager.StudentGradeSystem.pojo.Account;
import GradeManager.StudentGradeSystem.pojo.LoginDTO;
import GradeManager.StudentGradeSystem.pojo.PersonInfo;
import GradeManager.StudentGradeSystem.service.PersonService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Service
public class PersonServiceImp implements PersonService {
    @Autowired
    private PersonDao personDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //添加个人信息
    @Override
    public void addInfo(PersonInfo infoData) {
        Integer loginId = (Integer) BaseContent.getLoginClaims().get("loginId");
        String loginName = (String) BaseContent.getLoginClaims().get("loginName");
        Integer loginStatus = (Integer) BaseContent.getLoginClaims().get("loginStatus");
        infoData.setUserId(loginId);
        //初始化基本信息
        infoData.setUserName(loginName);
        infoData.setUserStatus(loginStatus);
        log.info("添加的个人信息:{}",infoData);
//        保存填入的数据
        personDao.insert(infoData);
    }

    @Override
    public boolean getInfo() {
//        根据登录的id获取个人信息
        LambdaQueryWrapper<PersonInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PersonInfo::getUserId,BaseContent.getLoginClaims().get("loginId"));
        PersonInfo personInfo = personDao.selectOne(wrapper);
        if(personInfo==null){
            return false;
        }else return true;
    }

    @Override
    public PersonInfo lookInfo(HttpServletRequest request) {
        Claims loginClaims = BaseContent.getLoginClaims();
        Integer loginId=(Integer) loginClaims.get("loginId");
        String loginName=(String)loginClaims.get("loginName");
        Integer loginStatus =(Integer) loginClaims.get("loginStatus");
        LambdaQueryWrapper<PersonInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PersonInfo::getUserId, loginId);
        PersonInfo personInfo = personDao.selectOne(wrapper);
        //判断个人信息是否已添加
        if(personInfo==null){
//            如果未添加则显示基本信息
            personInfo=new PersonInfo();
            personInfo.setUserId(loginId);
            personInfo.setUserName(loginName);
            personInfo.setUserStatus(loginStatus);
        }
//        否则显示已添加的信息
        return personInfo;
    }

    @Override
    public void modifyInfo(PersonInfo infoData) {
        //根据id修改个人信息
        LambdaQueryWrapper<PersonInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PersonInfo::getUserId,BaseContent.getLoginClaims().get("loginId"));
        personDao.update(infoData,wrapper);
    }

    @Override
    public PersonInfo reshow() {
//        回显已保存的信息
        LambdaQueryWrapper<PersonInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PersonInfo::getUserId,BaseContent.getLoginClaims().get("loginId"));
        PersonInfo prePersonInfo = personDao.selectOne(wrapper);
        return prePersonInfo;
    }
}
